﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TerhelesEloszto.Common;

namespace TerhelesEloszto.Engine
{
	/// <summary>
	/// Egy időintervallumot reprezentál.
	/// </summary>
	class Interval : IInterval
	{

		#region IInterval Members

		/// <summary>
		/// A hét napja.
		/// </summary>
		public DayOfWeek Day
		{
			get { return day; }
			set { day = value; }
		}

		/// <summary>
		/// Az intervallum kezdete.
		/// </summary>
		public int BeginHour
		{
			get { return beginHour; }
			set { beginHour = value; }
		}

		/// <summary>
		/// Az intervallum vége.
		/// </summary>
		public int EndHour
		{
			get { return endHour; }
			set { endHour = value; }
		}

		/// <summary>
		/// Páros vagy páratlan hét. (true = páratlan)
		/// </summary>
		public bool Parity
		{
			get { return parity; }
			set { parity = value; }
		}

		/// <summary>
		/// Az intervallumhoz tartozó feladat.
		/// </summary>
		public ITask RealtedTask
		{
			get { return relatedTask; }
			set { relatedTask = value; }
		}

		/// <summary>
		/// Az intervallum azonositója.
		/// </summary>
		public int ID
		{
			get { return id; }
		}

		/// <summary>
		/// Az intervallumhoz tartozó feladat.
		/// </summary>
		/// <returns>Az intervallumhoz tartozó feladat.</returns>
		public ITask GetRealtedTask()
		{
			return RealtedTask;
		}


		/// <summary>
		/// Igaz, ha a megadott intervallummal van metszete.
		/// </summary>
		/// <param name="other">A másik intervallum.</param>
		/// <returns>Igaz, ha a megadott intervallummal van metszete.</returns>
		public bool HasIntersect( IInterval other )
		{
			if( Day != other.Day ) return false;
			if( Parity != other.Parity ) return false;

			if( ( BeginHour >= other.BeginHour ) && ( BeginHour < other.EndHour ) ) return true;
			if( ( other.BeginHour >= BeginHour ) && ( other.BeginHour < EndHour ) ) return true;

			return false;
		}


		#endregion

		/// <summary>
		/// Üres konstruktor.
		/// </summary>
		internal Interval() { }

		/// <summary>
		/// Konstruktor linq entitázsból.
		/// </summary>
		/// <param name="interval">A linq entitázs.</param>
		internal Interval( INTERVAL interval )
		{
			day = Engine.Instance().ConvertDay( interval.WEEK_DAY );

			beginHour = interval.BEGINHOUR;
			endHour = interval.ENDHOUR;

			if( interval.PAIRITY == 0 )
			{
				parity = false;
			}
			else
			{
				parity = true;
			}

			id = interval.ID;
		}

		private DayOfWeek day;
		private int beginHour;
		private int endHour;
		private bool parity;
		private ITask relatedTask;
		private int id;

		public override bool Equals( object obj )
		{
			if( obj is IInterval )
			{
				IInterval o = (IInterval)( obj );
				return o.ID == id;
			}
			else
			{
				return base.Equals( obj );
			}
		}

	}
}
